﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbDeliverDal
	{
		public DbDeliverDal()
		{
		}
 		#region Insert
 		public static int Insert(DbDeliver dbDeliver)
		{	
			string sql = @"Insert Into DbDeliver (
					DCode,
					DName,
					DUrl,
					DAPI,
					DFee,
					DStatus
				) values('{0}','{1}','{2}','{3}',{4},{5})";
			sql = string.Format(sql,
					SqlHelper.String(dbDeliver.DCode),
					SqlHelper.String(dbDeliver.DName),
					SqlHelper.String(dbDeliver.DUrl),
					SqlHelper.String(dbDeliver.DAPI),
					dbDeliver.DFee,
					dbDeliver.DStatus
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbDeliver dbDeliver)
		{	
			string sql = @"Update DbDeliver set 

					DCode= '{0}',
					DName= '{1}',
					DUrl= '{2}',
					DAPI= '{3}',
					DFee= {4},
					DStatus= {5}
				where

					DId= {6}";
			sql = string.Format(sql,
					SqlHelper.String(dbDeliver.DCode),
					SqlHelper.String(dbDeliver.DName),
					SqlHelper.String(dbDeliver.DUrl),
					SqlHelper.String(dbDeliver.DAPI),
					dbDeliver.DFee,
					dbDeliver.DStatus,
					dbDeliver.DId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 dId)
		{
		
			string sql = @"Delete from DbDeliver where 
					DId= {0}";
			sql = string.Format(sql,
					dId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbDeliver a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbDeliver Select(Int32 dId)
		{
			DbDeliver dbDeliver = null;
			string sql = @"select a.* from DbDeliver a  where
					a.DId= {0}
";
			sql = string.Format(sql,
					dId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbDeliver = new DbDeliver(ds.Tables[0].Rows[0]);					
			return dbDeliver;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbDeliver a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbDeliver a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbDeliver a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
